<template>
  <div>
    <!-- {{ props.modelValue[props.fileName] }} -->
    <el-upload
      class="avatar-uploader"
      action="https://storebms.supore.cn/upload"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
    </el-upload>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

import { handleApiResponse } from "@/utils/request.js";
import { UploadFile } from "../../wailsjs/go/api/FileSystem.js";

onMounted(() => {
  imageUrl.value = props.modelValue[props.fileName] || "";
});

const props = defineProps({
  modelValue: {
    type: Object,
    default: () => {},
  },
  fileName: {
    type: String,
    default: "headUrl",
  },
});

const imageUrl = ref(props.modelValue[props.fileName] || ""); // 图片URL

// 上传成功的回调函数
const handleAvatarSuccess = (response, file, fileList) => {
  ElMessage.success("上传成功");
  imageUrl.value = response.data.path || ""; // 生成图片URL
  // console.log(response);
  props.modelValue[props.fileName] = response.data.path || "";
};

// 上传之前的回调函数，用于验证文件类型
const beforeAvatarUpload = (file) => {
  // const isJPG = file.type === "image/jpeg";
  // if (!isJPG) {
  //   ElMessage.error("上传的图片只能是JPG格式");
  // }
  // return isJPG;
};
</script>

<style  lang="scss">
:deep(.el-upload--picture-card) {
  width: 6rem !important;
  height: 6rem !important;
}

.avatar-uploader .avatar {
  width: 6rem !important;
  height: 6rem !important;
  display: block;
}

.avatar-uploader .el-upload {
  border: 0.05rem dashed #ccc;
  border-radius: 0.2rem;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--theme-color);
}

.el-icon.avatar-uploader-icon {
  font-size: 1rem;
  color: #8c939d;
  width: 6rem !important;
  height: 6rem !important;
  text-align: center;

  &:hover {
    color: var(--theme-color);
  }
}
</style>